<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日期计算器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 20px;
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center; /* 水平居中 */
            height: 100vh; /* 高度设置为视口高度 */
            background-image: url("img/3.webp");
            background-size: 550px; /* 背景图片大小 */
            background-position: center; /* 背景图片位置 */
        }
        .container {
            text-align: center; /* 内容居中 */
            background-color: white; /* 背景颜色 */
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px; /* 为不同容器添加间距 */
            width: 300px; /* 固定宽度 */
        }
        h1 {
            color: #2196F3; /* 修改标题颜色为蓝色 */
            font-size: 1.5em; /* 调整标题大小 */
            margin-bottom: 15px; /* 标题与输入框的间距 */
        }
        input[type="date"],
        input[type="text"] {
            padding: 10px;
            margin: 10px 0;
            border: 2px solid #2196F3; /* 修改边框颜色为蓝色 */
            border-radius: 5px;
            width: calc(100% - 20px); /* 自适应宽度 */
            transition: border-color 0.3s;
            font-size: 1em; /* 设置字体大小 */
        }
        input[type="date"]:focus,
        input[type="text"]:focus {
            border-color: #1976D2; /* 修改聚焦时的边框颜色 */
            outline: none;
        }
        button {
            padding: 10px 15px;
            background-color: #2196F3; /* 修改按钮背景颜色为蓝色 */
            border: none;
            border-radius: 5px;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s;
            font-size: 1em; /* 设置字体大小 */
            width: 100%; /* 按钮宽度自适应 */
        }
        button:hover {
            background-color: #1976D2; /* 修改按钮悬停背景颜色 */
        }
        #chax,
        #zhiding {
            font-weight: bold;
            font-size: 1.2em;
            color: #2196F3; /* 修改结果显示颜色为蓝色 */
        }
    </style>
    <script>
        function riqicha() {
            let kaishi = document.querySelector("#kaishiriqi").value;
            kaishi = new Date(kaishi);
            let jieji = document.querySelector("#jiejieriqi").value;
            jieji = new Date(jieji);
            kaishi = kaishi.getTime();
            jieji = jieji.getTime();
            let cha = Math.round((jieji - kaishi) / (24 * 3600 * 1000));
            document.querySelector("#chax").innerHTML = cha;
        }
        function qianhou() {
            let riqi = document.querySelector("#riqi").value;
            riqi = new Date(riqi);
            let tianshu = document.querySelector("#qianhou").value;
            let zhiding = "";
            tianshu = Math.round(tianshu);
            if (tianshu < 0) {
                let qian = new Date(riqi.getTime() - tianshu * 24 * 3600 * 1000);
                let week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
                let day = week[qian.getDay()];
                zhiding = qian.toLocaleDateString() + " " + day;
            } else if (tianshu > 0) {
                let hou = new Date(riqi.getTime() + tianshu * 24 * 3600 * 1000);
                let week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
                let day = week[hou.getDay()];
                zhiding = hou.toLocaleDateString() + " " + day;
            } else {
                zhiding = "同一天";                                                                                                            
            }
            document.querySelector("#zhiding").innerHTML = zhiding;
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>计算日期差</h1>
        开始日期：<input type="date" id="kaishiriqi">
        <br>
        结束日期：<input type="date" id="jiejieriqi">
        <br>
        相差<span id="chax">0</span>天
        <br>
        <button onclick="riqicha()">计算日期差</button>
    </div>
    <div class="container">
        <h1>计算前后日期</h1>
        日期：<input type="date" id="riqi">
        <br>
        相差<br>
        <input type="text" id="qianhou">天
        <br>
        是:<span id="zhiding">0</span>
        <button onclick="qianhou()">计算前后日期</button>
        <p><a href="ceshihe.html">点击返回首页</a></p>
        <p>© 2024 周露航</p>
    </div>
</body>
</html>
